package com.ruoyi.security.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfMonitoring;
import com.ruoyi.security.domain.vo.AfMonitoringVo;
import com.ruoyi.security.service.IAfMonitoringService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.RemoteDictService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.domain.RemoteSysDictData;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频监控Controller
 *
 * @author Felix
 * @date 2021-10-12
 */
@Api(tags = " 视频监控")
@RestController
@RequestMapping("/afMonitoring")
public class AfMonitoringController extends BaseController {
    @Autowired
    private IAfMonitoringService afMonitoringService;

    @Autowired
    private RemoteDictService dictService;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 小程序查询监控基础列表
     */
    @ApiOperation(value = "小程序查询监控基础列表", notes = "小程序查询监控基础列表")
    @PreAuthorize(hasPermi = "security:afMonitoring:list")
    @GetMapping("/appList")
    public AjaxResult appList(AfMonitoringVo afMonitoringVo) {

        AfMonitoring afMonitoring = new AfMonitoring();
        BeanUtils.copyProperties(afMonitoringVo, afMonitoring);
        List<AfMonitoring> list = afMonitoringService.selectAfMonitoringList(afMonitoring);
        R<List<RemoteSysDictData>> r = dictService.getDictDatas("monitoring_type");
        List<RemoteSysDictData> dicts = new ArrayList<>();

        List<AfMonitoringVo> result = new ArrayList<>();
        if (r.getCode() == R.SUCCESS) {
            dicts = r.getData();
        }
        list.stream().forEach(o -> {
            AfMonitoringVo v = new AfMonitoringVo();
            BeanUtils.copyProperties(o, v);
            result.add(v);
        });
        dicts.stream().forEach(o -> {
            result.stream().forEach(m -> {
                if (m.getAidType().equals(o.getDictValue())) {
                    m.setTypeDesc(o.getDictLabel());
                }
            });
        });
        return AjaxResult.success(result);
    }


    /**
     * 查询视频监控列表
     */
    @ApiOperation(value = "查询视频监控列表", notes = "查询视频监控列表")
    @PreAuthorize(hasPermi = "security:afMonitoring:list")
    @GetMapping("/list")
    public TableDataInfo list(AfMonitoring afMonitoring) {
        startPage();
        List<AfMonitoring> list = afMonitoringService.selectAfMonitoringList(afMonitoring);

        return getDataTable(list);
    }

    /**
     * 导出视频监控列表
     */
    @ApiOperation(value = "导出视频监控列表", notes = "导出视频监控列表")
    @PreAuthorize(hasPermi = "security:afMonitoring:export")
    @Log(title = "视频监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfMonitoring afMonitoring) throws IOException {
        List<AfMonitoring> list = afMonitoringService.selectAfMonitoringList(afMonitoring);
        ExcelUtil<AfMonitoring> util = new ExcelUtil<AfMonitoring>(AfMonitoring.class);
        util.exportExcel(response, list, "视频监控数据");
    }

    /**
     * 获取视频监控详细信息
     */
    @ApiOperation(value = "获取视频监控详细信息", notes = "获取视频监控详细信息")
    @PreAuthorize(hasPermi = "security:afMonitoring:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afMonitoringService.selectAfMonitoringById(id));
    }

    /**
     * 新增视频监控
     */
    @ApiOperation(value = "新增视频监控", notes = "新增视频监控")
    @PreAuthorize(hasPermi = "security:afMonitoring:add")
    @Log(title = "视频监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfMonitoring afMonitoring) {
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(afMonitoring.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        remoteSysBmType.getBmId();//编码类型Id
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        afMonitoring.setAidCode(nowCode);
        afMonitoring.setPropertyDesc(deviceType);
        return toAjax(afMonitoringService.insertAfMonitoring(afMonitoring));
    }

    /**
     * 修改视频监控
     */
    @ApiOperation(value = "修改视频监控", notes = "修改视频监控")
    @PreAuthorize(hasPermi = "security:afMonitoring:edit")
    @Log(title = "视频监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfMonitoring afMonitoring) {
        return toAjax(afMonitoringService.updateAfMonitoring(afMonitoring));
    }

    /**
     * 删除视频监控
     */
    @ApiOperation(value = "删除视频监控", notes = "删除视频监控")
    @ApiImplicitParam(name = "ids", value = "视频监控ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afMonitoring:remove")
    @Log(title = "视频监控", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afMonitoringService.deleteAfMonitoringByIds(ids));
    }
}
